package com.nimitz.web.controller.module.employee;

import com.nimitz.common.annotation.Log;
import com.nimitz.common.core.controller.BaseController;
import com.nimitz.common.core.domain.AjaxResult;
import com.nimitz.common.core.page.TableDataInfo;
import com.nimitz.common.enums.BusinessType;
import com.nimitz.common.utils.StringUtils;
import com.nimitz.common.utils.poi.ExcelUtil;
import com.nimitz.module.employee.domain.EmpEmployee;
import com.nimitz.module.employee.service.IEmpEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 人员维护
 */
@RestController
@Api(tags = "人员管理-人员维护")
@RequestMapping("/module/employee/list")
public class EmpEmployeeController extends BaseController {

    @Resource
    private IEmpEmployeeService employeeService;

    /**
     * 新增人员
     *
     * @param employee 人员
     * @return 结果
     */
    @PostMapping
    @ApiOperation(value = "新增人员")
    @PreAuthorize("@ss.hasPermi('employee:list:add')")
    @Log(title = "人员管理", businessType = BusinessType.INSERT)
    public AjaxResult add(@RequestBody EmpEmployee employee) {
        if (StringUtils.isNotEmpty(employee.getEmployeeIdCard()) && !employeeService.checkEmployeeIdCardUnique(employee)) {
            return error("新增人员'" + employee.getEmployeeName() + "'失败，证件号码已存在!");
        } else if (StringUtils.isNotEmpty(employee.getEmployeePhone()) && !employeeService.checkEmployeePhoneUnique(employee)) {
            return error("新增人员'" + employee.getEmployeeName() + "'失败，联系电话已存在!");
        }
        employee.setCreateBy(getUsername());
        return toAjax(employeeService.insertEmployee(employee));
    }

    /**
     * 批量删除人员
     *
     * @param employeeIds 需要删除的人员id集合
     * @return 结果
     */
    @DeleteMapping("/{employeeIds}")
    @ApiOperation(value = "批量删除人员")
    @PreAuthorize("@ss.hasPermi('employee:list:remove')")
    @Log(title = "人员管理", businessType = BusinessType.DELETE)
    public AjaxResult removes(@PathVariable Integer[] employeeIds) {
        return toAjax(employeeService.deleteEmployeeByIds(getUsername(), employeeIds));
    }

    /**
     * 修改人员
     *
     * @param employee 人员
     * @return 结果
     */
    @PutMapping
    @ApiOperation(value = "修改人员")
    @PreAuthorize("@ss.hasPermi('employee:list:edit')")
    @Log(title = "人员管理", businessType = BusinessType.UPDATE)
    public AjaxResult edit(@RequestBody EmpEmployee employee) {
        employee.setUpdateBy(getUsername());
        return toAjax(employeeService.updateEmployee(employee));
    }

    /**
     * 根据人员id查询人员
     *
     * @param employeeId 人员id
     * @return 人员
     */
    @GetMapping("/{employeeId}")
    @ApiOperation(value = "根据人员id查询人员")
    public AjaxResult getInfo(@PathVariable(value = "employeeId", required = false) Integer employeeId) {
        AjaxResult ajax = AjaxResult.success();
        if (StringUtils.isNotNull(employeeId)) {
            EmpEmployee employee = employeeService.selectEmployeeById(employeeId);
            ajax.put(AjaxResult.DATA_TAG, employee);
        }
        return ajax;
    }

    /**
     * 根据条件查询人员列表（含分页）
     *
     * @param employee 人员
     * @return 人员集合
     */

    @GetMapping("/list")
    @ApiOperation(value = "根据条件查询人员列表（含分页）")
    @PreAuthorize("@ss.hasPermi('employee:list:list')")
    public TableDataInfo list(EmpEmployee employee) {
        startPage();
        List<EmpEmployee> list = employeeService.selectEmployeeList(employee);
        return getDataTable(list);
    }

    /**
     * 批量提交
     *
     * @param employeeIds 需要提交的人员id集合
     * @return 结果
     */
    @PutMapping("/submit/{employeeIds}")
    @ApiOperation(value = "批量提交")
    @PreAuthorize("@ss.hasPermi('employee:list:submit')")
    @Log(title = "人员管理", businessType = BusinessType.UPDATE)
    public AjaxResult submit(@PathVariable Integer[] employeeIds) {
        return toAjax(employeeService.submitEmployee(getUsername(), employeeIds));
    }

    @Log(title = "人员管理", businessType = BusinessType.EXPORT)
    // @PreAuthorize("@ss.hasPermi('system:user:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, EmpEmployee employee) {
        List<EmpEmployee> list = employeeService.selectEmployeeList(employee);
        ExcelUtil<EmpEmployee> util = new ExcelUtil<EmpEmployee>(EmpEmployee.class);
        util.exportExcel(response, list, "人员数据");
    }

    /**
     * 批量驳回
     *
     * @param employeeIds 需要驳回的人员id集合
     * @return 结果
     */
//    @PutMapping("/reject/{employeeIds}")
//    @ApiOperation(value = "批量驳回")
//    @Log(title = "人员管理", businessType = BusinessType.UPDATE)
//    public AjaxResult reject(@PathVariable Integer[] employeeIds) {
//        return toAjax(employeeService.rejectEmployee(getUsername(), employeeIds));
//    }
}
